/*
 * Powered By [template-generator]
 * Web Site: template-generator
 * Since 2014 - 2015
 * 
 */

package com.template.app.control;



import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cn.template.component.page.PagerHtmlModel;
import org.cn.template.component.page.PagerUtil;
import org.cn.template.component.utils.ServletUtil;
import org.cn.template.component.web.UrlTag;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.template.app.model.Category;
import com.template.app.service.ICategoryService;

/**
 * ClassName:CategoryController <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason	:TODO ADD REASON. <br/>
 * Date     :2015年9月27日 下午5:25:02 <br/>
 * @author  :http://my.oschina.net/httpssl
 * @email   :491835898@QQ.COM
 * @since   :JDK 1.7
 * @see 	 
 */
@Controller
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@RequestMapping("/category")
public class CategoryController
{
	//默认后缀 jsp
	//ServletUtil.getURI_with_suffix("/cat") = cat.jsp
	//UrlTag.getUrl("/cat") = cat.jsp

	ModelAndView LIST_ACTION = new ModelAndView(new RedirectView(UrlTag.getUrl("/category")));
    
	@Resource
	ICategoryService categoryService;
	
	/**
	 * 增加了@ModelAttribute的方法可以在本controller方法调用前执行,可以存放一些共享变量,如枚举值,或是一些初始化操作
	 * @throws Exception 
	 */
	@ModelAttribute
	public void init(ModelMap model) throws Exception {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
		model.put("cats",this.categoryService.selectList(null));
	}	
		
	/** 列表 */
	@RequestMapping
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String,Object> paramMap = ServletUtil.getPageParameterMap(request);
		ModelAndView listView = new ModelAndView(ServletUtil.BASE_TEMPLATE_PATH_ADMIN+"/category/list");
		PagerHtmlModel pager =  PagerUtil.getPagerHtmlModel(this.categoryService.getPagerModel(paramMap),
				ServletUtil.getPageParameterMap(request),ServletUtil.getURI_with_suffix("/category"));
		listView.addObject("pager",pager);
		return listView;
	}

	/** 显示 */
	@RequestMapping(value="/{category_id}")
	public String show(ModelMap model,@PathVariable java.lang.Long category_id) throws Exception {
		Map<String,Object> param = Maps.newHashMap();
		param.put("category_id",category_id);
		Category cat =  this.categoryService.selectById(param);
		model.addAttribute("category",cat);
		return ServletUtil.BASE_TEMPLATE_PATH_ADMIN+"category/show";
	}

	/** 进入新增 */
	@RequestMapping(value="/new")
	public String _new(ModelMap model,Category category,HttpServletRequest request,HttpServletResponse response) throws Exception {
		model.addAttribute("category",category);
		return ServletUtil.BASE_TEMPLATE_PATH_ADMIN+"/category/new";
	}
	
	/** 保存新增 */
	/** 可以考虑,加入@Valid*/
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public ModelAndView save(ModelMap model,Category category,HttpServletRequest request,HttpServletResponse response) throws Exception {
		this.categoryService.insertOne(category);
		return LIST_ACTION;
	}
	
	/** 编辑信息 */
	@RequestMapping(value="/edit/{cat_id}")
	public String edit(ModelMap model,@PathVariable java.lang.Long category_id) throws Exception {
		Map<String,Object> param = Maps.newHashMap();
		param.put("category_id",category_id);
		Category cat =  this.categoryService.selectById(param);
		model.addAttribute("category",cat);
		return ServletUtil.BASE_TEMPLATE_PATH_ADMIN+"/category/edit";
	}
	
	/** 保存更新 */
	@RequestMapping(value="/update/{cat_id}",method=RequestMethod.POST)
	public ModelAndView update(@PathVariable java.lang.Long cat_id,Category cat,HttpServletRequest request,HttpServletResponse response) throws Exception {
		this.categoryService.updateById(cat);
		return LIST_ACTION;
	}
	
	/** 删除 */
	@RequestMapping(value="/delete/{category_id}",method=RequestMethod.GET)
	public ModelAndView delete(ModelMap model,@PathVariable java.lang.Long category_id) throws Exception {
		Map<String,Object> param = Maps.newHashMap();
		param.put("category_id",category_id);
		this.categoryService.deleteById(param);
		return LIST_ACTION;
	}
	
	@ResponseBody
	@RequestMapping(value="/all_json",method=RequestMethod.POST)
	public String all_cat_json(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String json  = JSON.toJSONString(this.categoryService.selectList(null));
		return json;
	}
	
}